Skip to content

Return one node per match from firstChild() - #109

Merged
jakejackson1 merged 2 commits into
mainfrom
fix/issue-78
Oct 26, 2026
Merged

jakejackson1 merged 2 commits into
mainfrom
fix/issue-78

Conversation

@gravitypdf-bot

Copy link
Copy Markdown
Contributor

firstChild() broke out of its outer loop as soon as one match had a child element, so it returned at most one node for the whole match set. lastChild() returns one node per match.

$xml = '<?xml version="1.0"?><root><p><i>a</i><i>b</i></p><p><i>c</i><i>d</i></p></root>';

qp($xml, 'p')->firstChild()->textImplode(','); // before: 'a'    after: 'a,c'
qp($xml, 'p')->lastChild()->textImplode(',');  // 'b,d' (unchanged)

Behaviour change: firstChild() now returns the first child element of every match. To get the old result, use firstChild()->first().

Changes

  • Drop the $flag and the outer break in QueryFilters::firstChild(), and remove a stale comment
  • Update the firstChild() and lastChild() DocBlocks to say they return one child element per match
  • Add a CHANGELOG entry

Tests

testFirstChildReturnsOneChildPerMatch matches four elements: one empty, one with only text, one with text before its child elements, and one with a comment before its child element. It fails on main (it gets 'a') and passes with this change. phpunit, lint and lint:min-php all pass.

Fixes #78.

🤖 Generated with Claude Code

firstChild() broke out of its outer loop after the first match that had
a child element, so it returned at most one node. It now returns one per
match, like lastChild().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.79%. Comparing base (6ae6acf) to head (b750a3b).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #109      +/-   ##
============================================
+ Coverage     90.69%   90.79%   +0.10%     
+ Complexity     1424     1423       -1     
============================================
  Files            25       25              
  Lines          3170     3163       -7     
============================================
- Hits           2875     2872       -3     
+ Misses          295      291       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jakejackson1 jakejackson1 added the bug Something isn't working label Oct 26, 2026
@jakejackson1
jakejackson1 merged commit d36a5c3 into main Oct 26, 2026
12 checks passed
@jakejackson1
jakejackson1 deleted the fix/issue-78 branch October 26, 2026 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

firstChild() returns at most one node regardless of how many elements are selected

2 participants